package com.stu.dao;

import com.stu.beans.Student;
import com.stu.utils.JDBCUtil;

import java.util.List;

public class StuDao {

    public int addStu(Student stu) {
        String sql = "insert into t_stu(sno,name,age,gender,phone) values(?,?,?,?,?)";
        return JDBCUtil.update(sql, stu.getSno(), stu.getName(), stu.getAge(), changeGender(stu.getGender()), stu.getPhone());
    }

    public List<Student> queryStu(Student stu) {
        String sql = "select sno,name,age,gender,phone from t_stu where sno like ? and name like ? and phone like ?";
        System.out.println("执行查询的sql是"+stu.getSno()+"  "+stu.getName()+"  "+stu.getPhone());
        return JDBCUtil.queryList(sql, Student.class, stu.getSno(), stu.getName(), stu.getPhone());
    }

    public List<Student> queryAll() {
        String sql = "select sno,name,age,gender,phone from t_stu";
        return JDBCUtil.queryList(sql, Student.class);
    }

    public int modifyStu(Student student) {
        String sql = "update t_stu set name=?,age=?,gender=?,phone=? where sno=?";
        return JDBCUtil.update(sql, student.getName(), student.getAge(),changeGender(student.getGender()),student.getPhone(),student.getSno());
    }

    private int changeGender(String param){
        int gender = 2;
        switch (param){
            case "男":
                gender = 0;
                break;
            case "女":
                gender = 1;
                break;
        }
        return gender;
    }
}
